| Conditions | 1 |
| Paths | 1 |
| Total Lines | 99 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | jQuery( function( $ ) { |
||
| 2 | |||
| 3 | /** |
||
| 4 | * JS meta_box field actions. |
||
| 5 | */ |
||
| 6 | var cmb2MetaBoxField = { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Initialize meta box fields behavior. |
||
| 10 | */ |
||
| 11 | init: function() { |
||
| 12 | this.showHideOnLoad(); |
||
| 13 | this.fieldTypeChange(); |
||
| 14 | this.fieldNameChange(); |
||
| 15 | this.displayUsageFunctions(); |
||
| 16 | }, |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Show / hide field rows based on field_type_select selected value. |
||
| 20 | * |
||
| 21 | * showHide |
||
| 22 | * |
||
| 23 | * |
||
| 24 | * |
||
| 25 | */ |
||
| 26 | showHide: function( fieldTypeSelect ) { |
||
| 27 | var fieldTypeVal = $( fieldTypeSelect ).val(); |
||
| 28 | var fieldSet = $( fieldTypeSelect ).closest( '.cmb-field-list' ); |
||
| 29 | $( '.cmb-row.cmb_hide_field', fieldSet ).hide(); |
||
| 30 | $( '.cmb-row.' + fieldTypeVal, fieldSet ).show(); |
||
| 31 | }, |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Use the showHide() function on each field group's field rows on page load. |
||
| 35 | * |
||
| 36 | * showHideOnLoad |
||
| 37 | * |
||
| 38 | * |
||
| 39 | * |
||
| 40 | */ |
||
| 41 | showHideOnLoad: function() { |
||
| 42 | var fieldTypeSelects = $( '.field_type_select' ); |
||
| 43 | fieldTypeSelects.each( function() { |
||
| 44 | cmb2MetaBoxField.showHide( this ); |
||
| 45 | } ); |
||
| 46 | |||
| 47 | }, |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Use the showHide() function on this field group's field rows on field_type_select change. |
||
| 51 | * |
||
| 52 | * fieldTypeChange |
||
| 53 | * |
||
| 54 | * |
||
| 55 | * |
||
| 56 | */ |
||
| 57 | fieldTypeChange: function() { |
||
| 58 | $( '.cmb2-metabox' ).on( 'change', '.field_type_select', function( e ) { |
||
| 59 | var fieldTypeSelect = e.target; |
||
| 60 | cmb2MetaBoxField.showHide( fieldTypeSelect ); |
||
| 61 | } ); |
||
| 62 | }, |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Update the usage code snippets when field_name changes. |
||
| 66 | * |
||
| 67 | * fieldNameChange |
||
| 68 | * |
||
| 69 | * |
||
| 70 | * |
||
| 71 | */ |
||
| 72 | fieldNameChange: function() { |
||
| 73 | $( '.cmb2-metabox' ).on( 'change keyup', '.field_name', function( e ) { |
||
|
|
|||
| 74 | this.displayUsageFunctions(); |
||
| 75 | }.bind( this ) ); |
||
| 76 | }, |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Convert field name to usage code snippet. |
||
| 80 | * |
||
| 81 | * displayUsageFunctions |
||
| 82 | * |
||
| 83 | * |
||
| 84 | * |
||
| 85 | */ |
||
| 86 | displayUsageFunctions: function() { |
||
| 87 | var fieldName = $( '.field_name' ); |
||
| 88 | var wrapper = fieldName.closest( '.cmb-field-list' ); |
||
| 89 | var getPostMeta = $( '.get_post_meta', wrapper ); |
||
| 90 | var cmbf = $( '.cmbf', wrapper ); |
||
| 91 | var fieldNameVal = fieldName.val().toLowerCase().replace( / /g, '_' ); |
||
| 92 | getPostMeta.val( 'get_post_meta( get_the_ID(), \'_' + fieldNameVal + '\', true );'); |
||
| 93 | cmbf.val( 'cmbf( get_the_ID(), \'_' + fieldNameVal + '\' );'); |
||
| 94 | } |
||
| 95 | }; |
||
| 96 | |||
| 97 | cmb2MetaBoxField.init(); |
||
| 98 | |||
| 99 | } ); |
||
| 100 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.